package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.dictionary.FeedBackStatusEnum;
import com.book.model.dto.FeedBackQueryDTO;
import com.book.model.entity.Books;
import com.book.model.entity.Feedback;
import com.book.mapper.FeedbackMapper;
import com.book.model.entity.User;
import com.book.model.vo.FeedBackMainVO;
import com.book.model.vo.FeedBackVO;
import com.book.service.FeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 问题反馈表 服务实现类
 * </p>
 *
 * @author auto
 * @since 2025-04-02 06:38:55
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Autowired
    private UserService userService;
    @Override
    public List<FeedBackVO> getFeedbackList() {
        List<Feedback> feedbackList = this.list();
        if(CollectionUtils.isEmpty(feedbackList)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(User::getUserId,feedbackList.stream().map(Feedback::getUserId).collect(Collectors.toList()));
        Map<String,User> userMap = userService.list(queryWrapper).stream().collect(Collectors.toMap(User::getUserId,user->user));
        List<FeedBackVO> feedBackVOList = new ArrayList<>();
        feedbackList.forEach(feedback->{
            FeedBackVO vo = new FeedBackVO();
            BeanUtils.copyProperties(feedback,vo);
            vo.setNickName(userMap.get(feedback.getUserId()).getNickName());
            vo.setAvatarUrl(userMap.get(feedback.getUserId()).getAvatarUrl());
            feedBackVOList.add(vo);
        });
        return feedBackVOList;
    }

    @Override
    public List<FeedBackVO> getFeedbackListById(String userId) {
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Feedback::getUserId,userId);
        List<Feedback> feedbackList = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(feedbackList)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<User> queryUserWrapper = new LambdaQueryWrapper<>();
        queryUserWrapper.in(User::getUserId,feedbackList.stream().map(Feedback::getUserId).collect(Collectors.toList()));
        Map<String,User> userMap = userService.list(queryUserWrapper).stream().collect(Collectors.toMap(User::getUserId,user->user));
        List<FeedBackVO> feedBackVOList = new ArrayList<>();
        feedbackList.forEach(feedback->{
            FeedBackVO vo = new FeedBackVO();
            BeanUtils.copyProperties(feedback,vo);
            vo.setNickName(userMap.get(feedback.getUserId()).getNickName());
            vo.setAvatarUrl(userMap.get(feedback.getUserId()).getAvatarUrl());
            feedBackVOList.add(vo);
        });
        return feedBackVOList;
    }

    @Override
    public FeedBackVO addFeedBack(Feedback back) {
        back.setCreateTime(new Date());
        back.setFeedbackDate(new Date());
        back.setStatusCode(FeedBackStatusEnum.PENDING.getCode());
        back.setStatusName(FeedBackStatusEnum.PENDING.getName());
        this.save(back);
        FeedBackVO vo = new FeedBackVO();
        BeanUtils.copyProperties(back,vo);
        User user = userService.getById(back.getUserId());
        vo.setNickName(user.getNickName());
        vo.setAvatarUrl(user.getAvatarUrl());
        return vo;
    }

    @Override
    public boolean dealFeedBack(Feedback back) {
        Feedback updateBack = this.getById(back.getFeedbackId());
        updateBack.setDealMessage(back.getDealMessage());
        updateBack.setUpdateTime(new Date());
        updateBack.setStatusName(FeedBackStatusEnum.FINISH.getCode());
        updateBack.setStatusName(FeedBackStatusEnum.FINISH.getName());
        return this.updateById(updateBack);
    }

    @Override
    public FeedBackMainVO getFeedbackListByParam(FeedBackQueryDTO dto) {
        FeedBackMainVO feedBackMainVO = new FeedBackMainVO();
        List<FeedBackVO> feedBackVOList = new ArrayList<>();
        Page<Feedback> feedbackPage = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getSearch())){
            queryWrapper.and(wrapper -> wrapper
                    .like(Feedback::getFeedbackContent, dto.getSearch())
                    .or()
                    .like(Feedback::getStatusName, dto.getSearch())
            );
        }
        Page<Feedback> list = this.page(feedbackPage,queryWrapper);
        if(CollectionUtils.isEmpty(list.getRecords())){
            feedBackMainVO.setTotal(0);
        feedBackMainVO.setRecords(feedBackVOList);
            return feedBackMainVO;
        }
        LambdaQueryWrapper<User> queryUserWrapper = new LambdaQueryWrapper<>();
        queryUserWrapper.in(User::getUserId,list.getRecords().stream().map(Feedback::getUserId).collect(Collectors.toList()));
        Map<String,User> userMap = userService.list(queryUserWrapper).stream().collect(Collectors.toMap(User::getUserId,user->user));
        list.getRecords().forEach(feedback->{
            FeedBackVO vo = new FeedBackVO();
            BeanUtils.copyProperties(feedback,vo);
            BeanUtils.copyProperties(userMap.get(feedback.getUserId()),vo);
            feedBackVOList.add(vo);
        });
        feedBackMainVO.setTotal((int) list.getTotal());
        feedBackMainVO.setRecords(feedBackVOList);
        return feedBackMainVO;
    }
}
